﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utilities;

namespace Repository
{
    public class ServerHardDataUtil
    {
        public static DataTable buildDashboardsDataTable()
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode("HARDATA");
            xmlGetter.setLine1Node("DASHBOARD");
            xmlGetter.setLine2Node(Utilities.GlobalRunning.strSERVER_BeingChosen);
            xmlGetter.setLine3Node("variable");

            DataTable dtTarget = xmlGetter.getLine3NodeValues();
            DataRow DR = dtTarget.Rows[0];
            string stringListOfDashboards = DR[1].ToString().Trim();
            string[] strArrayOfDashboards = stringListOfDashboards.Split(',');

            //Build datatable
            DataTable dtDashboards = new DataTable();
            dtDashboards.Columns.Add(new DataColumn("Attribute1", typeof(string)));
            dtDashboards.Columns.Add(new DataColumn("Attribute2", typeof(string)));

            //Add blank row first
            DataRow drowDashboardBLANK = dtDashboards.NewRow();
            drowDashboardBLANK[0] = "";
            drowDashboardBLANK[1] = "";
            dtDashboards.Rows.Add(drowDashboardBLANK);

            //Add found data
            int i = 0;
            while (i < strArrayOfDashboards.Length)
            {
                string strDashboardItem = strArrayOfDashboards[i].Trim();
                if (!strDashboardItem.Equals(""))
                {
                    DataRow drowDashboardItem = dtDashboards.NewRow();
                    drowDashboardItem[0] = strDashboardItem;
                    drowDashboardItem[1] = strDashboardItem;
                    dtDashboards.Rows.Add(drowDashboardItem);
                }
                i++;
            }

            return dtDashboards;
        }

        public static DataTable buildProfilesDataTable()
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode("HARDATA");
            xmlGetter.setLine1Node("PROFILES");
            xmlGetter.setLine2Node(Utilities.GlobalRunning.strSERVER_BeingChosen);
            xmlGetter.setLine3Node("variable");

            DataTable dtTarget = xmlGetter.getLine3NodeValues();
            DataRow DR = dtTarget.Rows[0];
            string stringListOfProfiles = DR[1].ToString().Trim();
            string[] strArrayOfProfiles = stringListOfProfiles.Split(',');

            //Build datatable
            DataTable dtProfiles = new DataTable();
            dtProfiles.Columns.Add(new DataColumn("Attribute1", typeof(string)));
            dtProfiles.Columns.Add(new DataColumn("Attribute2", typeof(string)));

            //Add blank row first
            DataRow drowDashboardBLANK = dtProfiles.NewRow();
            drowDashboardBLANK[0] = "";
            drowDashboardBLANK[1] = "";
            dtProfiles.Rows.Add(drowDashboardBLANK);

            //Add found data
            int i = 0;
            while (i < strArrayOfProfiles.Length)
            {
                string strDashboardItem = strArrayOfProfiles[i].Trim();
                if (!strDashboardItem.Equals(""))
                {
                    DataRow drowDashboardItem = dtProfiles.NewRow();
                    drowDashboardItem[0] = strDashboardItem;
                    drowDashboardItem[1] = strDashboardItem;
                    dtProfiles.Rows.Add(drowDashboardItem);
                }
                i++;
            }

            return dtProfiles;
        }

        public static DataTable buildChartsDataTable()
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode("HARDATA");
            xmlGetter.setLine1Node("CHARTS");
            xmlGetter.setLine2Node("variable");

            DataTable dtTarget = xmlGetter.getLine2NodeValues();
            return dtTarget;
        }

        public static DataTable buildChartTypeDataTable()
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode("HARDATA");
            xmlGetter.setLine1Node("CHARTTYPE");
            xmlGetter.setLine2Node("variable");

            DataTable dtTarget = xmlGetter.getLine2NodeValues();
            return dtTarget;
        }

        public static DataTable buildCategoriesDataTable()
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode(Utilities.GlobalRunning.strSERVER_BeingChosen);
            xmlGetter.setLine1Node("CATEGORY");
            xmlGetter.setLine2Node("variable");

            DataTable dtTarget = xmlGetter.getLine2NodeValues();
            return dtTarget;
        }

        public static void saveDashboardName(string strDBName)
        {
            DataTable dtDashboards = new DataTable();
            dtDashboards.Columns.Add(new DataColumn("Attribute1", typeof(string)));
            dtDashboards.Columns.Add(new DataColumn("Attribute2", typeof(string)));
            dtDashboards = buildDashboardsDataTable();

            string stringListOfDashboards = "";
            Boolean flag_found = false;
            foreach (DataRow DR in dtDashboards.Rows)
            {
                string strCurrentCheckDashboard = DR[0].ToString().Trim();
                if (strCurrentCheckDashboard.Equals(strDBName))
                {
                    flag_found = true;
                    break;
                }
                stringListOfDashboards += strCurrentCheckDashboard + ",";
            }

            //Check if NOT found, we will add this one as new
            if (!flag_found) stringListOfDashboards += strDBName;

            //Clean redundant data
            while (stringListOfDashboards.Contains(",,"))
            {
                stringListOfDashboards = stringListOfDashboards.Replace(",,", ",");
            }

            while (stringListOfDashboards.Contains(", ,"))
            {
                stringListOfDashboards = stringListOfDashboards.Replace(", ,", ",");
            }

            XMLSetter xmlSetter = new XMLSetter();
            xmlSetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlSetter.setMasterNode("SM2");
            xmlSetter.setHeaderNode("HARDATA");
            xmlSetter.setLine1Node("DASHBOARD");
            xmlSetter.setLine2Node(Utilities.GlobalRunning.strSERVER_BeingChosen);
            xmlSetter.setLine3Node("variable");
            xmlSetter.setLine3NodeValue("name", "ListOfDashboards", "value", stringListOfDashboards);
        }

        public static void removeDashboardName(string strDBName)
        {
            DataTable dtDashboards = new DataTable();
            dtDashboards.Columns.Add(new DataColumn("Attribute1", typeof(string)));
            dtDashboards.Columns.Add(new DataColumn("Attribute2", typeof(string)));
            dtDashboards = buildDashboardsDataTable();

            string stringListOfDashboards = "";
            foreach (DataRow DR in dtDashboards.Rows)
            {
                string strCurrentCheckDashboard = DR[0].ToString().Trim();
                if (!strCurrentCheckDashboard.Equals(strDBName))
                {
                    stringListOfDashboards += strCurrentCheckDashboard + ",";
                }
            }

            XMLSetter xmlSetter = new XMLSetter();
            xmlSetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlSetter.setMasterNode("SM2");
            xmlSetter.setHeaderNode("HARDATA");
            xmlSetter.setLine1Node("DASHBOARD");
            xmlSetter.setLine2Node(Utilities.GlobalRunning.strSERVER_BeingChosen);
            xmlSetter.setLine3Node("variable");
            xmlSetter.setLine3NodeValue("name", "ListOfDashboards", "value", stringListOfDashboards);
        }

        public static void saveProfiles(string stringProfiles)
        {
            DataTable dtProfiles = new DataTable();
            dtProfiles.Columns.Add(new DataColumn("Attribute1", typeof(string)));
            dtProfiles.Columns.Add(new DataColumn("Attribute2", typeof(string)));
            dtProfiles = buildProfilesDataTable();

            string[] listProfiles = stringProfiles.Split(',');
            for (int i = 0; i < listProfiles.Length; i++)
            {
                string strCurrentCheckProfile = listProfiles[i].Trim();
                if (!strCurrentCheckProfile.Equals(""))
                {
                    Boolean found = false;

                    //loop over all already saved profile to check if the current one was existed or not
                    foreach (DataRow DR in dtProfiles.Rows)
                    {
                        string strPrf = DR[0].ToString().Trim();
                        if (strPrf.Equals(strCurrentCheckProfile))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found) 
                        saveProfileName(strCurrentCheckProfile);
                }
            }
        }

        public static void saveProfileName(string strProfileName)
        {
            DataTable dtProfiles = new DataTable();
            dtProfiles.Columns.Add(new DataColumn("Attribute1", typeof(string)));
            dtProfiles.Columns.Add(new DataColumn("Attribute2", typeof(string)));
            dtProfiles = buildProfilesDataTable();

            string stringListOfProfiles = "";
            Boolean flag_found = false;
            foreach (DataRow DR in dtProfiles.Rows)
            {
                string strCurrentCheckProfile = DR[0].ToString().Trim();
                if (strProfileName.Equals(strCurrentCheckProfile))
                {
                    flag_found = true;
                    break;
                }
                stringListOfProfiles += strCurrentCheckProfile + ",";
            }

            //Check if NOT found, we will add this one as new
            if (!flag_found) stringListOfProfiles += strProfileName;

            //Clean redundant data
            while (stringListOfProfiles.Contains(",,"))
            {
                stringListOfProfiles = stringListOfProfiles.Replace(",,", ",");
            }

            while (stringListOfProfiles.Contains(", ,"))
            {
                stringListOfProfiles = stringListOfProfiles.Replace(", ,", ",");
            }

            XMLSetter xmlSetter = new XMLSetter();
            xmlSetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlSetter.setMasterNode("SM2");
            xmlSetter.setHeaderNode("HARDATA");
            xmlSetter.setLine1Node("PROFILES");
            xmlSetter.setLine2Node(Utilities.GlobalRunning.strSERVER_BeingChosen);
            xmlSetter.setLine3Node("variable");
            xmlSetter.setLine3NodeValue("name", "ListOfProfiles", "value", stringListOfProfiles);
        }

        public static void deleteProfileName(string strProfileName)
        {
            DataTable dtProfiles = new DataTable();
            dtProfiles.Columns.Add(new DataColumn("Attribute1", typeof(string)));
            dtProfiles.Columns.Add(new DataColumn("Attribute2", typeof(string)));
            dtProfiles = buildProfilesDataTable();

            string stringListOfProfiles = "";
            foreach (DataRow DR in dtProfiles.Rows)
            {
                string strCurrentCheckProfile = DR[0].ToString().Trim();
                if (!strCurrentCheckProfile.Equals(strProfileName))
                {
                    stringListOfProfiles += strCurrentCheckProfile + ",";
                }
            }

            XMLSetter xmlSetter = new XMLSetter();
            xmlSetter.setXmlFilePath(Utilities.GlobalFile.SERVER_HARDATA_FILENAME);
            xmlSetter.setMasterNode("SM2");
            xmlSetter.setHeaderNode("HARDATA");
            xmlSetter.setLine1Node("PROFILES");
            xmlSetter.setLine2Node(Utilities.GlobalRunning.strSERVER_BeingChosen);
            xmlSetter.setLine3Node("variable");
            xmlSetter.setLine3NodeValue("name", "ListOfProfiles", "value", stringListOfProfiles);
        }
    }
}
